class EventEmitter {
  constructor() {
    this.events = {}
  }
  on(type, callback) {
    if (!this.events[type]) {
      this.events[type] = []
    }
    this.events[type].push(callback)
  }
  emit(type, ...args) {
    this.events[type] && this.events[type].forEach(fn => {
      fn.apply(this, args)
    })
  }
  once(type, callback) {
    function fn() {
      callback()
      this.off(type, fn)
    }
    this.on(type, fn)
  }
  off(type, callback) {
    if (this.events[type]) {
      this.events[type] = this.events[type].filter(e => e !== callback)
    }
  }
}

let e = new EventEmitter()
e.on('click', function () {
  console.log('click1');
})
e.on('click', function () {
  console.log('click2');
})
e.on('dbclick', function () {
  console.log('dbclick');
})
function test() {
  console.log('test');
}
e.on('click', test)
e.off('click', test)
e.once('click', function () {
  console.log('once');
})
console.log(e);
e.emit('click')
e.emit('dbclick')
e.emit('click')

console.log(e);
